package guodaowang.apidata;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class GetAPIData1 {
    public static void main(String[] args) throws Exception {
        String data = getData();
        strtojson(data);


    }
    public static String getData() throws Exception {
        //请求地址
        String path ="https://restapi.amap.com/v3/place/text?s=rsv3&children=&key=" +
                "8325164e247e15eea68b59e89200988b&page=1&offset=10&city=110000&language=" +
                "zh_cn&callback=jsonp_870417_&platform=JS&logversion=2.0&sdkversion=1.3&appname=" +
                "https%3A%2F%2Flbs.amap.com%2Fconsole%2Fshow%2Fpicker&csid=7133E38E-149A-4CD1-B32B-F5B343CA5571" +
                "&keywords=潘所服务区";
        URL url = new URL(path);
        //获取链接
        URLConnection conn = url.openConnection();
        //获取返回值
        InputStream inputStream = conn.getInputStream();
        //转换成String类型
        String response = IOUtils.toString(inputStream);

        String[] split = response.split("\\(");
        String s = split[1];
        String substring = s.substring(0, s.length() - 1);
        return substring;

    }

    public static void strtojson(String str) throws Exception {

        if (StringUtils.isNotEmpty(str)) {
            JSONObject jsonObject = JSON.parseObject(str);
            JSONArray jsonArray = jsonObject.getJSONArray("pois");
            for(int i=0;i<jsonArray.size();i++){
                System.out.print(jsonArray.getJSONObject(i).get("name")+":");
                System.out.println(jsonArray.getJSONObject(i).get("location"));
            }

        }


    }
}
